const app = getApp();
const util = require("../../../../../utils/util");
var appId;
var path;
var model_id;
let toggleCollection = true;
Page({
    data: {
        toMiniProgram: false,
        scroll: false,
        desc: "",
    },
    onLoad: function(options) {
        toggleCollection = true;
        if (options.url != undefined) {
            this.setData({
                url: options.url,
            });
        }
        if (options.title != undefined) {
            this.setData({
                title: options.title,
            });
        }

        if (options.desc) {
            this.setData({
                desc: options.desc
            });
        }

        util.getUrl(app.globalData.url).then(res => {
            model_id = options.model_id;
            let $getMiniProgramInfo = {
                action: "model",
                model_id
            };
            wx.request({
                url: app.globalData.api,
                data: $getMiniProgramInfo,
                method: "GET",
                header: { "content-type": "application/json" },
                success: res => {
                    let miniProgramInfo = JSON.parse(JSON.stringify(res.data))
                    if (miniProgramInfo.wx_appid && miniProgramInfo.wx_path) {
                        this.setData({
                            toMiniProgram: true,
                        });
                        appId = miniProgramInfo.wx_appid;
                        path = miniProgramInfo.wx_path;
                    }
                },
            })
        });
    },
    toMiniProgram() {
        wx.navigateToMiniProgram({
            appId,
            path,
        });
    },
    onShareAppMessage: function() {
        return {
            path: `/pages/tools/banshi/problem/item/item?model_id=${model_id}`,
            title: app.globalData.shareTitle ? app.globalData.shareTitle : "本地宝",
            imageUrl: app.globalData.shareImage ? app.globalData.shareImage : "http://imgbdb3.bendibao.com/xcxbdb/file/20212/9/20210209161843_68677.jpg",
        }
    },

    // 分享朋友圈
    onShareTimeline: function() {
        return {
            title: app.globalData.shareTitle ? app.globalData.shareTitle : "本地宝",
            imageUrl: app.globalData.shareImage ? app.globalData.shareImage : "http://imgbdb3.bendibao.com/xcxbdb/file/20212/9/20210209161843_68677.jpg",
            query: `/pages/tools/banshi/problem/item/item?model_id=${model_id}`,
        }
    },
    onPageScroll() {
        if (toggleCollection) {
            this.setData({
                scroll: true,
            });
            toggleCollection = false;
        }

    },
    closeCollection() {
        toggleCollection = false;
    },
    openCollection() {
        toggleCollection = true;
    }
});